package site.wanjiahao.tree;

import java.util.Comparator;


// 二叉搜索树
public class BST<E> extends BinaryTree<E> {

    private final Comparator<E> comparator;

    public BST(Comparator<E> comparator) {
        this.comparator = comparator;
    }

    public BST() {
        this(null);
    }

    public void add(E ele) {
        if (root == null) {
            // 添加root节点
            root = createNode(ele, null);
            size++;
            addAfter(root);
        } else {
            Node cur = root;
            Node parent = null;
            int flag = 0;

            while (cur != null) {
                int compareRes = comparator(cur.ele, ele);
                if (compareRes > 0) {
                    parent = cur;
                    cur = cur.left;
                    flag = 0;
                } else if (compareRes < 0) {
                    parent = cur;
                    cur = cur.right;
                    flag = 1;
                } else {
                    // 覆盖处理，ele可能为引用类型，更加符合实际情况
                    cur.ele = ele;
                    return;
                }
            }
            Node setNode = createNode(ele, parent);
            if (flag == 0) {
                parent.left = setNode;
            } else {
                parent.right = setNode;
            }
            size++;
            addAfter(setNode);
        }
    }

    protected void addAfter(Node node) {

    }

    protected void removeAfter(Node node, Node replacement) {

    }

    public int comparator(E e1, E e2) {
        if (comparator != null) {
            return comparator.compare(e1, e2);
        }
        // 直接强制转换，默认实现比较接口
        return ((Comparable<E>) e1).compareTo(e2);
    }

    public void remove(E ele) {
        // 判断当前节点的度
        Node node = node(ele);
        if (node == null) return;

        if (hasTwoChildren(node)) {
            // 度为2：先找到对应的前驱（后继）节点，覆盖删除的节点，然后在删除对应的前驱（后继）节点（只存在度为1或者度为2）
            Node successor = successor(node);
            // predecessor不存在为空
            node.ele = successor.ele;
            node = successor;
        }

        // 度为1：当前节点为父节点的什么节点，当前节点的子节点，作为这个节点存在
        Node replacement = node.left != null ? node.left : node.right;
        if (replacement != null) {
            replacement.parent = node.parent;
            // 度为1
        }
        if (node.isLeftNode()) {
            node.parent.left = replacement;
        } else if (node.isRightNode()) {
            node.parent.right = replacement;
        } else {
            root = replacement;
        }
        size--;
        removeAfter(node, replacement);
    }

    public boolean contains(E ele) {
        return node(ele) != null;
    }

    // 根据元素值，获取节点对象
    public Node node(E ele) {
        Node node = root;
        while (node != null) {
            int comp = comparator(node.ele, ele);
            if (comp > 0) {
                // 左边
                node = node.left;
            } else if (comp < 0) {
                // 右边
                node = node.right;
            } else {
                return node;
            }
        }
        return null;
    }

}
